package com.talent.controller;

import com.talent.common.PageResult;
import com.talent.common.Result;
import com.talent.pojo.entity.*;
import com.talent.pojo.vo.HomeSettingsVO;
import com.talent.pojo.vo.MenuContentDetailVO;
import com.talent.pojo.vo.MenuTreeVO;
import com.talent.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/common")
@Slf4j
public class CommonController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SpotService spotService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private HomeService homeService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private NoticeBannerService noticeBannerService;

    /**
     * 获取首页设置
     */
    @GetMapping("/settings")
    public Result<HomeSettingsVO> getHomeSettings() {
        log.info("获取首页设置");
        HomeSettingsVO homeSettings = homeService.getHomeSettings();
        return Result.success(homeSettings);
    }

    /**
     * 获取顶级菜单列表
     */
    @GetMapping("/top-menu")
    public Result<List<Menu>> getTopLevelMenus() {
        log.info("获取顶级菜单列表");
        List<Menu> topLevelMenus = menuService.getTopLevelMenus();
        return Result.success(topLevelMenus);
    }

    /**
     * 根据父ID获取所有子菜单列表（递归获取所有层级的子项）
     */
    @GetMapping("/menu/{parentId}")
    public Result<List<Menu>> getChildMenusByParentId(@PathVariable Long parentId) {
        log.info("根据父ID获取所有子菜单列表（递归）：{}", parentId);
        List<Menu> childMenus = menuService.getChildMenusByParentId(parentId);
        return Result.success(childMenus);
    }

    /**
     * 根据类型查询分类列表
     */
    @GetMapping("/categories")
    public Result<List<Category>> getByType(@RequestParam(required = true) String type) {
        log.info("根据类型查询分类列表：{}", type);
        List<Category> categories = categoryService.getByType(type);
        return Result.success(categories);
    }

    /**
     * 获取筛选列表
     */
    @GetMapping("/filterList")
    public Result<Map<String, List>> getFilterList() {
        List<Category> publicList = categoryService.getByType("public");
        List<Category> serviceList = categoryService.getByType("service");
        List<Category> shareList = categoryService.getByType("share");
        Map<String, List> map = new HashMap<>();
        map.put("publicList", publicList);
        map.put("serviceList", serviceList);
        map.put("shareList", shareList);
        return Result.success(map);
    }

    /**
     * 获取点位列表
     */
    @GetMapping("/spots")
    public Result<List<Spot>> getSpots() {
        return Result.success(spotService.getSpots());
    }

    /**
     * 根据ID查询点位
     */
    @GetMapping("/spots/{id}")
    public Result<Spot> getById(@PathVariable Long id) {
        log.info("根据ID查询点位：{}", id);
        Spot spot = spotService.getById(id);
        return Result.success(spot);
    }

    /**
     * 获取服务树形数据
     */
    @GetMapping("/serviceTree")
    public Result<List<MenuTreeVO>> getMenuTree(@RequestParam(required = false) String name) {
        log.info("获取菜单树形结构");
        List<MenuTreeVO> menuTree = menuService.getMenuTree(name);
        return Result.success(menuTree);
    }

    /**
     * 根据ID查询菜单内容详情和顶层父级信息
     * 主要用于服务内容项，获取详细信息和层级关系
     */
    @GetMapping("/services/{id}")
    public Result<MenuContentDetailVO> getMenuById(@PathVariable Long id) {
        log.info("根据ID查询菜单内容详情和顶层父级信息：{}", id);
        MenuContentDetailVO menuDetail = menuService.getContentDetailById(id);
        return Result.success(menuDetail);
    }

    /**
     * 分页查询通知
     */
    @GetMapping("/notices")
    public Result<PageResult<Notice>> pageQuery(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer pageSize,
                                                String title) {
        log.info("分页查询通知：page={}, pageSize={}, title={}", page, pageSize, title);
        PageResult<Notice> pageResult = noticeService.pageQuery(page, pageSize, title);
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询通知
     */
    @GetMapping("/notices/{id}")
    public Result<Notice> getNoticesById(@PathVariable Long id) {
        log.info("根据ID查询通知：{}", id);
        Notice notice = noticeService.getById(id);
        return Result.success(notice);
    }

    /**
     *
     */
    @GetMapping("/notice-banners")
    public Result<List<NoticeBanner>> getList() {
        log.info("获取轮播列表");
        List<NoticeBanner> list = noticeBannerService.getWechatList();
        return Result.success(list);
    }
}
